package wps.component.elementsetting {
import flash.events.Event;

import spark.components.Image;
import spark.components.Label;

import wps.component.DrawingImage;
import wps.component.DrawingText;

/**
 * 输入高度和宽度
 */
public class SettingSizeTextInput extends SettingTextInput implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // byPercent
    //----------------------------------

    private var _byPercent:Boolean;

    /**
     * 是否使用百分比设置
     */
    public function get byPercent():Boolean {
        return _byPercent;
    }

    public function set byPercent(byPercent:Boolean):void {
        _byPercent = byPercent;
        sizeModeChanged();
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    private function sizeModeChanged():void {
        if (byPercent) {
            percentMode();
        } else {
            text = target[targetProperty];
        }
    }

    private function percentMode():void {
        if (!target) {
            text = "";
            return;
        }

        if (target is Image) {
            var image:DrawingImage = target as DrawingImage;
            text = Math.round(target[targetProperty] / image.bitmapData[targetProperty] * 100).toString();
        } else if (target is DrawingText) {
            // 文字长宽不能设置，肯定是100%
            text = "100";
        }
    }

    //--------------------------------------------------------------------------
    //
    // Override Methods
    //
    //--------------------------------------------------------------------------

    override protected function textSetter(value:Object):void {
        if (byPercent) {
            percentMode();
        } else {
            super.textSetter(value);
        }
    }

    override protected function inputCompleted():void {
        if (byPercent) {
            var image:Image = target as Image;
            if (image) {
                var value:Number = Number(text);
                if (!isNaN(value)) {
                    target[targetProperty] = Math.round(image.bitmapData[targetProperty] * value / 100);
                }
            }
        } else {
            super.inputCompleted();
        }
    }

}
}
